import * as types from './mutation-types'
import systemAPI from "@/api/system.js"
// #ifndef APP-NVUE
import {uploadFile} from "@/utils/file.js"
// #endif
// #ifdef APP-PLUS
import {getVersion,existApp} from '@/utils/util.js'
// #endif
export default {
	async initSystem({commit}){
		const configs = await systemAPI.getBatchConfig()
		
		// #ifndef APP-NVUE
		uploadFile.canal = configs['sys.cat.upload.type']
		// #endif
		const nameEdit = configs['sys.userName.isUp']||'1'
		commit(types.UPDATE_NICK_EDIT,nameEdit==1)
		const mineQuan = JSON.parse(configs['sys.redMine.num']||'[{"num":7,"odds":1.6},{"num":8,"odds":1.4},{"num":9,"odds":1.2}]') 
		commit(types.UPDATE_MINE_QUAN,mineQuan)
		const mineRange = configs['sys.redMine.money']||'1-1000'
		const [min,max] = mineRange.split('-')
		commit(types.UPDATE_MINE_RANGE,{min:min*1,max:max*1})
		const rechargeable = configs['sys.wallet.recharge']=='1'
		commit(types.UPDATE_RECHARGEABLE,rechargeable)
		const cashMin = Number(configs['sys.mini.extract.money'])
		commit(types.UPDATE_CASH_MIN,cashMin)
		const withdraw = (configs['sys.wallet.withdraw']||'1')=='1'
		commit(types.UPDATE_WITHDRAW,withdraw)
		const redMax = Number(configs['sys.red.max']||'10000')
		commit(types.UPDATE_RED_MAX,redMax)
		const breaktimeStr = configs['cat.extract.ban.time']||'00:00-09:00'
		try{
			const [startStr,endStr] = breaktimeStr.split('-')
			const [sH,sM] = startStr.split(':')
			const [eH,eM] = endStr.split(':')
			const start = Number(sH)*60+Number(sM)
			const end = Number(eH)*60+Number(eM)
			if(isNaN(start)||isNaN(end)){
				throw new Error('时间格式错误')
			}
			commit(types.UPDATE_BREAKTIME,{start,end})
		}catch(e){
			commit(types.UPDATE_BREAKTIME,{start:0,end:540})
		}
		const auth = configs['sys.cat.auth'] 
		if(auth=='1'){ 
			commit(types.UPDATE_EXAMINE,true)
		}else{
			// #ifdef H5
			commit(types.UPDATE_EXAMINE,false) 
			// #endif
			// #ifdef APP-PLUS
			const systemInfo = uni.getSystemInfoSync()
			if(systemInfo.osName == 'ios'){ 
				const iosAuth = configs['sys.cat.ios.auth']||'' 
				const version =  systemInfo.appWgtVersion
				console.log('当前版本',version,iosAuth)
				
				if(iosAuth.slice(0,1)=='1'&&iosAuth.endsWith(version)&&!existApp(existApp.map.weixin)){
					commit(types.UPDATE_EXAMINE,true)
				}else{
					commit(types.UPDATE_EXAMINE,false)
				}
			}else{ 
				commit(types.UPDATE_EXAMINE,false) 
			}
			// #endif
		}
		return configs
	}
}
